
YarkoCooldowns = {}

YarkoCooldowns.DefaultMainColor = NORMAL_FONT_COLOR;
YarkoCooldowns.DefaultFlashColor = { r = 1.0, g = 0, b = 0 };
YarkoCooldowns.DefaultFontLocation = "Fonts";
YarkoCooldowns.DefaultFontFile = "FRIZQT__.TTF";
YarkoCooldowns.DefaultFontHeight = 20;

YarkoCooldowns.CounterFrames = {};


-- No longer using button list - now shows based on button width
local ButtonList = {
	"ActionButton",
	"BonusActionButton",
	"MultiBarBottomLeftButton",
	"MultiBarBottomRightButton",
	"MultiBarRightButton",
	"MultiBarLeftButton",
	"ShapeshiftButton",
	"PetActionButton",
	"VehicleMenuBarActionButton",
	"Character",
	"ContainerFrame",
	"SpellButton",
	"BT4Button",
	"BT4StanceButton",
	"DominosActionButton",
	"WatchFrameItem",
	"TellMeWhen_Group",
};

local Counters = {};

local Scales = {
	[1] = 1.3,
	[2] = 1.2,
	[3] = .8,
	[4] = .6
};

local TimeSinceLastUpdate = 1;
local UpdateInterval = .25;

local ButtonWidth = 28;


function YarkoCooldowns.OnLoad(self)
	self:RegisterEvent("PLAYER_LOGIN");
end


function YarkoCooldowns.OnEvent(self, event, ...)
	if (event == "PLAYER_LOGIN") then
        YarkoCooldowns.OptionsSetup();
        
		hooksecurefunc("CooldownFrame_SetTimer", YarkoCooldowns.CooldownSetTimer);
	end
end


function YarkoCooldowns.OnUpdate(self, elapsed)
	TimeSinceLastUpdate = TimeSinceLastUpdate + elapsed;
	
	if (TimeSinceLastUpdate > UpdateInterval) then
		for k, v in pairs(Counters) do
			local timeleft = ceil(v.start + v.duration - GetTime());
			countertext = _G["YarkoCooldowns_"..k.."CounterText"];
			
			if (timeleft > 0 and v.enable > 0) then
				if (_G[k]:IsVisible()) then
					local timeleftstr = YarkoCooldowns.GetTimeFormat(timeleft);
					
					if (timeleft <= 3) then
						if (not v.flash) then
							v.flash = true;
							v.flag = false;
						end
					else
						if (v.flash) then
							v.flash = false;
						end
					end
					
					if (timeleftstr ~= countertext:GetText() or v.flash) then
						local strlen = #timeleftstr;
						
						if (strlen > 4) then
							strlen = 4;
						end
						
						countertext:SetTextColor(YarkoCooldowns_SavedVars.MainColor.r, YarkoCooldowns_SavedVars.MainColor.g,
							YarkoCooldowns_SavedVars.MainColor.b);
						_G["YarkoCooldowns_"..k.."Counter"]:SetScale(Scales[strlen] * (_G[k]:GetParent():GetWidth() 
							/ ActionButton1:GetWidth()));
						countertext:SetText(timeleftstr);
						
						if (v.flash) then
							if (not v.flag) then
								countertext:SetTextColor(YarkoCooldowns_SavedVars.FlashColor.r, 
									YarkoCooldowns_SavedVars.FlashColor.g, YarkoCooldowns_SavedVars.FlashColor.b);
								v.flag = true;
							else
								v.flag = false;
							end
						end
					end
				else
					countertext:SetText("");
				end
			else
				Counters[k] = nil;
				countertext:SetText("");
			end
		end

		TimeSinceLastUpdate = 0;
	end
end


function YarkoCooldowns.CooldownSetTimer(self, start, duration, enable)
	if (self.mark == 0) then
		return;
	end
	
	local name = self:GetName();
	
	if (not self.mark) then
		if (not name) then
			self.mark = 0;
			return;
		end
		
		--self.mark = 1;
		--self.mark = ((YarkoCooldowns.InList(name) and 1) or 0);
		self.mark = ((self:GetParent():GetWidth() >= ButtonWidth and 1) or 0);
	end
	
	if (self.mark == 1) then
		local countername = "YarkoCooldowns_"..name.."Counter";
		
		if (start > 0 and duration > 1.5 and enable > 0) then
			if (not _G[countername]) then
				local frame = CreateFrame("Frame", countername, self, "YarkoCooldowns_CounterTemplate");
				tinsert(YarkoCooldowns.CounterFrames, countername); 
				YarkoCooldowns.UpdateFont(_G["YarkoCooldowns_"..name.."CounterText"]);
				frame:Show();
			end
			
			Counters[name] = {};
			Counters[name].start = start;
			Counters[name].duration = duration;
			Counters[name].enable = enable;
			Counters[name].flash = false;
			Counters[name].flag = false;
		else
			if (_G[countername]) then
				_G[countername.."Text"]:SetText("");
			end
			
			Counters[name] = nil;
		end
	end
end


function YarkoCooldowns.InList(name)
	for index, value in ipairs(ButtonList) do
		if (strsub(name, 1, strlen(value)) == value) then
			return true;
		end
	end
	
	return false;
end


function YarkoCooldowns.GetTimeFormat(time)
	if (time > 3600) then
		return format("%dh", ceil(time / 3600));
	elseif (time > 60) then
		return format("%dm", ceil(time / 60));
	else
		return format("%d", time);
	end
end
